
#include <BReadWrite>
#include "member_BStyle.h"
#include "member_BWidget.h"
#include "member_BObject.h"
#include "IGlobal.h"

using namespace BWE;

member_BStyle::member_BStyle(BStyle* style)
{
	boss = style;
	dirty = true;
	heritable = true;
}
member_BStyle::~member_BStyle()
{

}

void member_BStyle::init(const BColor& color)
{
	int gray = color.gray();
	if (gray > 127)
	{
		colorMap[Color_Title_Text] = BColor(0);
		colorMap[Color_Title_Text_Actived] = BColor(0);
		colorMap[Color_Title_Text_Checked] = BColor(20);
		colorMap[Color_Title_Text_Hovered] = BColor(15);
		colorMap[Color_Title_Text_Pressed] = BColor(10);
		colorMap[Color_Title_Text_Disable] = BColor(30);

		colorMap[Color_Text] = BColor(0);
		colorMap[Color_Text_Actived] = BColor(0);
		colorMap[Color_Text_Checked] = BColor(20);
		colorMap[Color_Text_Hovered] = BColor(15);
		colorMap[Color_Text_Pressed] = BColor(10);
		colorMap[Color_Text_Disable] = BColor(30);

		colorMap[Color_Background] = color % 96;
		colorMap[Color_Background_Actived] = color % 96;
		colorMap[Color_Background_Checked] = color % 66;
		colorMap[Color_Background_Hovered] = color % 86;
		colorMap[Color_Background_Pressed] = color % 76;
		colorMap[Color_Background_Disable] = color % 56;

		colorMap[Color_Border] = color % 80;
		colorMap[Color_Border_Actived] = color % 80;
		colorMap[Color_Border_Checked] = color % 76;
		colorMap[Color_Border_Hovered] = color % 70;
		colorMap[Color_Border_Pressed] = color % 80;
		colorMap[Color_Border_Disable] = color % 76;

		colorMap[Color_Perch] = color % 86;
		colorMap[Color_Perch_Actived] = color % 86;
		colorMap[Color_Perch_Checked] = color % 80;
		colorMap[Color_Perch_Hovered] = color % 96;
		colorMap[Color_Perch_Pressed] = color % 70;
		colorMap[Color_Perch_Disable] = color % 60;

		colorMap[Color_Handle] = color % 90;
		colorMap[Color_Handle_Actived] = color % 90;
		colorMap[Color_Handle_Checked] = color % 60;
		colorMap[Color_Handle_Hovered] = color % 70;
		colorMap[Color_Handle_Pressed] = color % 80;
		colorMap[Color_Handle_Disable] = color % 76;

		colorMap[Color_Arrow] = color % 10;
		colorMap[Color_Arrow_Actived] = color % 10;
		colorMap[Color_Arrow_Checked] = color % 36;
		colorMap[Color_Arrow_Hovered] = color % 28;
		colorMap[Color_Arrow_Pressed] = color % 15;
		colorMap[Color_Arrow_Disable] = color % 56;

		colorMap[Color_Tab_Checked] = color % 66;
		colorMap[Color_Tab_Hovered] = color % 86;
		colorMap[Color_Tab_Pressed] = color % 56;
		colorMap[Color_Tab_Disable] = color % 36;

		colorMap[Color_Header_Checked] = color % 66;
		colorMap[Color_Header_Hovered] = color % 86;
		colorMap[Color_Header_Pressed] = color % 56;
		colorMap[Color_Header_Disable] = color % 36;

		colorMap[Color_Item_Checked] = color % 60;
		colorMap[Color_Item_Hovered] = color % 66;
		colorMap[Color_Item_Pressed] = color % 56;
		colorMap[Color_Item_Disable] = color % 36;

		colorMap[Color_Prefix] = BColor(30);
		colorMap[Color_Prefix_Hovered] = BColor(50);
		colorMap[Color_Prefix_Pressed] = BColor(40);
		colorMap[Color_Prefix_Disable] = BColor(80);

		colorMap[Color_Suffix] = BColor(30);
		colorMap[Color_Suffix_Hovered] = BColor(50);
		colorMap[Color_Suffix_Pressed] = BColor(40);
		colorMap[Color_Suffix_Disable] = BColor(80);

		colorMap[Color_Wire] = color % 62;
		colorMap[Color_Indicator] = BColor(10);
		colorMap[Color_Chunk] = color % 60;
		colorMap[Color_Groove] = color % 60;
		colorMap[Color_Separator] = color % 76;
		colorMap[Color_Grid_Ver] = color % 62;
		colorMap[Color_Grid_Hor] = color % 62;

	}
	else
	{
		BColor textColor(255, 255, 255);

		colorMap[Color_Title_Text] = textColor;
		colorMap[Color_Title_Text_Actived] = textColor;
		colorMap[Color_Title_Text_Checked] = textColor;
		colorMap[Color_Title_Text_Hovered] = textColor % 120;
		colorMap[Color_Title_Text_Pressed] = textColor;
		colorMap[Color_Title_Text_Disable] = textColor;

		colorMap[Color_Text] = textColor;
		colorMap[Color_Text_Actived] = textColor;
		colorMap[Color_Text_Checked] = textColor;
		colorMap[Color_Text_Hovered] = textColor % 120;
		colorMap[Color_Text_Pressed] = textColor;
		colorMap[Color_Text_Disable] = textColor % 70;

		colorMap[Color_Background] = color % 56;
		colorMap[Color_Background_Actived] = color % 56;
		colorMap[Color_Background_Checked] = color % 76;
		colorMap[Color_Background_Hovered] = color % 86;
		colorMap[Color_Background_Pressed] = color % 66;
		colorMap[Color_Background_Disable] = color % 30;

		colorMap[Color_Border] = color % 120;
		colorMap[Color_Border_Actived] = color % 120;
		colorMap[Color_Border_Checked] = color % 156;
		colorMap[Color_Border_Hovered] = color % 166;
		colorMap[Color_Border_Pressed] = color % 130;
		colorMap[Color_Border_Disable] = color % 50;

		colorMap[Color_Perch] = color;
		colorMap[Color_Perch_Actived] = color;
		colorMap[Color_Perch_Checked] = color % 120;
		colorMap[Color_Perch_Hovered] = color % 130;
		colorMap[Color_Perch_Pressed] = color % 110;
		colorMap[Color_Perch_Disable] = color % 150;

		colorMap[Color_Handle] = color % 150;
		colorMap[Color_Handle_Actived] = color % 150;
		colorMap[Color_Handle_Checked] = color % 140;
		colorMap[Color_Handle_Hovered] = color % 166;
		colorMap[Color_Handle_Pressed] = color % 130;
		colorMap[Color_Handle_Disable] = color % 50;

		colorMap[Color_Arrow] = textColor % 60;
		colorMap[Color_Arrow_Actived] = textColor % 60;
		colorMap[Color_Arrow_Checked] = textColor % 66;
		colorMap[Color_Arrow_Hovered] = textColor % 80;
		colorMap[Color_Arrow_Pressed] = textColor % 66;
		colorMap[Color_Arrow_Disable] = textColor % 36;

		colorMap[Color_Tab_Checked] = color % 25;
		colorMap[Color_Tab_Hovered] = color % 28;
		colorMap[Color_Tab_Pressed] = color % 18;
		colorMap[Color_Tab_Disable] = color % 18;

		colorMap[Color_Header_Checked] = color % 126;
		colorMap[Color_Header_Hovered] = color % 136;
		colorMap[Color_Header_Pressed] = color % 106;
		colorMap[Color_Header_Disable] = color % 66;

		colorMap[Color_Item_Checked] = color % 126;
		colorMap[Color_Item_Hovered] = color % 136;
		colorMap[Color_Item_Pressed] = color % 116;
		colorMap[Color_Item_Disable] = color % 66;

		colorMap[Color_Prefix] = textColor % 76;
		colorMap[Color_Prefix_Hovered] = textColor % 86;
		colorMap[Color_Prefix_Pressed] = textColor % 76;
		colorMap[Color_Prefix_Disable] = textColor % 66;

		colorMap[Color_Suffix] = textColor % 76;
		colorMap[Color_Suffix_Hovered] = textColor % 86;
		colorMap[Color_Suffix_Pressed] = textColor % 76;
		colorMap[Color_Suffix_Disable] = textColor % 66;

		colorMap[Color_Indicator] = textColor;
		colorMap[Color_Chunk] = color % 120;
		colorMap[Color_Groove] = color % 120;
		colorMap[Color_Separator] = textColor;
		colorMap[Color_Grid_Ver] = color % 186;
		colorMap[Color_Grid_Hor] = color % 186;
		colorMap[Color_Wire] = color % 186;
	}
}
bool member_BStyle::apply(BWidget* widget) const
{
	bool changed = false;
	for (int i = 0; i < children.size(); i++)
	{
		BStyle* child = children[i];
		const BString& type = style_member(child)->type;
		if (style_member(child)->heritable)
		{
			if (type.size() && !widget->typed(type))
				continue;
		}
		else
		{
			if (type != widget->className())
				continue;
		}
		const BString& name = object_member(child)->name;
		if (name.size() && name != widget->name())
			continue;
		if (!widget_member(widget)->realStyle)
		{
			widget_member(widget)->realStyle = new BStyle();
		}
		BStyle* realStyle = widget_member(widget)->realStyle;
		style_member(realStyle)->overlap(child);
		changed = true;
	}
	return changed;
}
void member_BStyle::overlap(const BStyle* style)
{
	if (style == boss)
		return;
	if (style == 0)
		return;
	if (style_member(style)->valueMap.size())
	{
		for (auto it = style_member(style)->valueMap.begin(); it.valid(); it++)
		{
			valueMap[it.key()] = it.value();
		}
	}
	if (style_member(style)->colorMap.size())
	{
		for (auto it = style_member(style)->colorMap.begin(); it.valid(); it++)
		{
			colorMap[it.key()] = it.value();
		}
	}
	if (style_member(style)->imageMap.size())
	{
		for (auto it = style_member(style)->imageMap.begin(); it.valid(); it++)
		{
			imageMap[it.key()] = it.value();
		}
	}
	if (style_member(style)->iconMap.size())
	{
		for (auto it = style_member(style)->iconMap.begin(); it.valid(); it++)
		{
			iconMap[it.key()] = it.value();
		}
	}
	if (style_member(style)->namedFontMap.size())
	{
		for (auto it = style_member(style)->namedFontMap.begin(); it.valid(); it++)
		{
			namedFontMap[it.key()] = it.value();
		}
	}
	if (style_member(style)->namedColorMap.size())
	{
		for (auto it = style_member(style)->namedColorMap.begin(); it.valid(); it++)
		{
			namedColorMap[it.key()] = it.value();
		}
	}
	if (style_member(style)->namedImageMap.size())
	{
		for (auto it = style_member(style)->namedImageMap.begin(); it.valid(); it++)
		{
			namedImageMap[it.key()] = it.value();
		}
	}
	if (style_member(style)->namedIconMap.size())
	{
		for (auto it = style_member(style)->namedIconMap.begin(); it.valid(); it++)
		{
			namedIconMap[it.key()] = it.value();
		}
	}
	if (style_member(style)->transMap.size())
	{
		for (auto it = style_member(style)->transMap.begin(); it.valid(); it++)
		{
			transMap[it.key()] = it.value();
		}
	}
	if (style_member(style)->subs.size())
	{
		for (int i = 0; i < style_member(style)->subs.size(); i++)
		{
			BStyle* subStyle = style_member(style)->subs[i];
			BStyle* sub = boss->sub(subStyle->name());
			if (sub)
			{
				style_member(sub)->overlap(subStyle);
			}
			else
			{
				sub = new BStyle(*subStyle);
				subs.append(sub);
			}
		}
	}
	if (style_member(style)->children.size())
	{
		for (int i = 0; i < style_member(style)->children.size(); i++)
		{
			BStyle* child = style_member(style)->children[i];
			if (children.lower(child))
				continue;
			children.append(child);
		}
	}
}
